import os
import random
import socket
import threading
import time

from xjlibrary.database_moudel.simple.mysqlclient import MySqlDbConnect, SelctSqlFromDB, ExeSqlToDB, ExeSqlList
from xjlibrary.mprocesspoll.MThreadingRun import MThreadingRun
from xjlibrary.mrequest.baserequest import MProxyRequest, BaseRequest
from xjlibrary.our_file_dir.base_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
sPath = BaseDir.get_new_path(TopPath, "download", "dqlib", "download", "archive")

HEADERS = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "accept-encoding": "gzip, deflate",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
    "upgrade-insecure-requests": "1",
    "Host": "dqlib.vip.qikan.com",
    "Proxy-Connection": "keep-alive",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 "
                  "Safari/537.36 "
}

baseurl = "http://dqlib.vip.qikan.com/text/"

proxyss = {
    "http": "192.168.30.36:8033",
    "https": "192.168.30.36:8033",
}


def requestVolIssue(url, proxysList):
    BoolResult, errString, r = MProxyRequest(url, Feature="facets-container", HEADERS=HEADERS, proxyRingList=proxysList,
                                             timeout=(30, 60))
    if not BoolResult:
        print("请检查失败原因:" + errString)
        return False
    return r


def savefile(line, filePath):
    BaseDir.single_add_file(filePath, line)


# 数据库链接
def MajorDbConnect():
    return MySqlDbConnect(curPath, "db.ini")


def SelectFromDB():
    sql = "select `url`,`filename`,`classtext` from `archive` where `stat`=0 limit 10000"
    conn = MajorDbConnect()
    rows = SelctSqlFromDB(sql, conn)
    return rows


def UpdateSql(sql):
    conn = MajorDbConnect()
    ExeSqlToDB(sql, conn, errExit=True)


def requestJournals(url):
    BoolResult, errString, r = BaseRequest(url,
                                           mark="mediabox",
                                           headers=HEADERS,
                                           proxies=proxyss,
                                           timeout=(30, 60))
    if not BoolResult:
        print("请检查失败原因:" + errString)
        return ""
    return r


# 多网卡情况下，根据前缀获取IP
def GetLocalIPByPrefix(prefix):
    localIP = ''
    for ip in socket.gethostbyname_ex(socket.gethostname())[2]:
        if ip.startswith(prefix):
            localIP = ip

    return localIP


def get_list_url_run(threadval, row):
    result_queue = threadval.result_queue
    urlfull = baseurl + row[0]
    outPathFile = os.path.join(sPath,
                               '%s_%d_%d.big_html' % (
                                   GetLocalIPByPrefix('192.168.'), os.getpid(), threading.get_ident())
                               )

    if not os.path.exists(sPath):
        os.makedirs(sPath)

    r = requestJournals(urlfull)
    if r == "":
        sql = "update `archive` set `stat`=0,failed = failed + 1 where url='{}'".format(row[0])
        result_queue.put(sql)
        return
    try:
        line = row[0] + ':' + row[1] + ":" + row[2] + '★' + \
               r.text.replace('\0', ' ').replace(
                   '\r', ' ').replace('\n', ' ') + '\n'
        savefile(line, outPathFile)
        sql = "update `archive` set `stat`=1 where url='{}'".format(row[0])
        result_queue.put(sql)
        print("保存文件成功")
    except:
        print("保存文件失败")
    time.sleep(random.randint(20, 30))


# 插入数据库
def InsertIntoDbFromList(ListSqls):
    conn = MajorDbConnect()
    success, failed = ExeSqlList(ListSqls, conn)
    ListSqls = list()


class IssueThreadRun(MThreadingRun):

    def __init__(self, num):
        super(IssueThreadRun, self).__init__(num)

    def getTask(self, *args, **kwargs):
        return SelectFromDB()

    def setTask(self, results=None, *args, **kwargs):
        for row in results:
            self.add_job(self.func, row)
        time.sleep(5)

    def dealresult(self, *args, **kwargs):
        print("self.results length is {}".format(len(self.results)))
        InsertIntoDbFromList(self.results)

    def setProxy(self, proxysList=None):
        time.sleep(60)

    def thread_pool_hook(self, thread_pool_dicts, thread, args, kwargs):
        return {}

    def is_break(self):
        return False

    def fun(self, threadval, *args, **kwargs):
        row = args[0]
        get_list_url_run(threadval, row)


if __name__ == "__main__":
    func = get_list_url_run
    threadrun = IssueThreadRun(1)
    threadrun.run(model=2)
